var path = require('path')
var webpack = require('webpack')
var CleanWebpackPlugin = require('clean-webpack-plugin')
var ExtractTextPlugin = require("extract-text-webpack-plugin")
var CopyWebpackPlugin = require('copy-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

var bundleType = process.env.NODE_ENV;
module.exports = {
  entry: ['babel-polyfill', './src/main.js'],
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: bundleType === 'production' ? '/' : '/dist/',
    filename: bundleType === 'production' ? 'js/[name].[chunkhash].js' : 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'css': bundleType === 'production' ? ExtractTextPlugin.extract('css-loader') : 'vue-style-loader!css-loader',
            'scss': bundleType === 'production' ? ExtractTextPlugin.extract('css-loader!sass-loader') : 'vue-style-loader!css-loader!sass-loader',
            'sass': bundleType === 'production' ? ExtractTextPlugin.extract('css-loader!scss-loader') : 'vue-style-loader!css-loader!sass-loader'
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true,
    proxy: {
      '/f/': {
        target: 'http://www.nmc.cn',
        secure: false
        // pathRewrite: {
        //   '^/weather': ''
        // }
      },
      '/door/': {
        target: 'http://10.42.113.26:8080',
        secure: false
        // pathRewrite: {
        //   '^/hgslnw': ''
        // }
      },
      '/publish/': {
        target: 'http://www.nmc.cn',
        secure: false
      },
      '/hgweb/': {
        target: 'http://61.136.242.44:8802',
        secure: false,
        pathRewrite: {
          '^/hgweb/default.aspx': '/hgweb/index.aspx'
        }
      }
    }
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (bundleType === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: false,
      mangle: true,
      compress: {
        warnings: false,
        drop_debugger: true,
        drop_console: true
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    }),
    new HtmlWebpackPlugin({
      title: '黄冈水利信息化市级平台',
      template: './template.html',
      favicon: './src/assets/img/favicon.ico'
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      // filename:'vendor.js',
      minChunks: function (module) {
        // 该配置假定你引入的 vendor 存在于 node_modules 目录中
        return module.context && module.context.indexOf('node_modules') !== -1;
      }
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest'
    }),
    // new webpack.LoaderOptionsPlugin({
    //   minimize: true
    // }),
    new ExtractTextPlugin({
      filename: "css/style.css",
      allChunks: true
    }),
    new CleanWebpackPlugin(['dist']),
    new CopyWebpackPlugin([{
      from: './src/assets',
      to: 'src/assets'
    },
    {
      from: './src/jsons',
      to: 'src/jsons'
    }
    ]),
    new BundleAnalyzerPlugin(),
  ])
}
